@extends('layouts.app')
@include('user.sidebar')
@section('content')
    <div class="content">
        <div class="row custom-invoice">
            <div class="">
                <form id="select-form" class="form form-inline" method="" action="">
                    <input type="hidden" name="level" value="{{ $level }}">
                    <input type="hidden" name="_token" value="{{ csrf_token() }}">
                    <div class="form-group">
                        <input class="form-control" type="text" name="search" id="" value="{{ $search }}" placeholder="电话/姓名/昵称">
                    </div>
                    @if($level == 2)
                        <div class="form-group">
                            <input class="form-control" type="text" name="searchReferrer" id="" value="{{ $searchReferrer }}" placeholder="邀请人">
                        </div>
                    @endif
                    <button type="button" class="btn btn-success select-button" data-action="{{ route('admin.user.index', ['level' => $level]) }}" value="">搜索</button>
                    @can("user-level{$level}-export")
                        <button type="button" class="btn btn-primary export" data-action="{{ route('admin.user.export') }}" value="">导出</button>
                    @endcan
                </form>
            </div>
            <br />

            @if($level == 2)
                @can('user-edit-referrer')
                    <br />
                    <button type="button" class="btn btn-primary batchChangeReferrer">批量修改邀请人</button>
                    <br />
                @endcan
            @endif
            <br />
            <span>数量：{{ $count }} </span>
            <table class="table table-striped table-hover">
                <thead>
                <tr>
                    <th style="min-width: 80px;">真实姓名</th>
                    <th style="min-width: 100px;">昵称</th>
                    <th style="min-width: 50px;">头像</th>
                    <th style="min-width: 90px;">身份</th>
                    <th style="min-width: 90px;">电话号</th>
                    <th style="min-width: 90px;">推荐人</th>
                    <th style="min-width: 100px;">推荐人级别</th>
                    <th style="min-width: 100px;">首次登陆时间</th>
                    <th style="min-width: 100px;">最近登陆时间</th>
                    @if($level == 2)
                        <th style="min-width: 70px;">邀请人</th>
                    @endif
                    <th style="min-width: 400px;">操作</th>
                </tr>
                </thead>
                <tbody>
                @foreach($data as $item)
                    <tr>
                        <td>{{ $item->name }}</td>
                        <td><a href="{{ route('admin.order.good_index', ['referrer_id' => $item->id]) }}" target="_blank">{{ $item->nickname }}</a></td>
                        <td><img src="{{ $item->avatar }}" width="30"></td>
                        <td>{{ \App\Modules\User\Models\User::$levelShow[$item->level] }}</td>
                        <td>{{ $item->mobile }}</td>
                        <td>
                            @if(!is_null($tmp = $referrer->get($item->referrer_id)))
                                <a href="{{ route('admin.user.index', ['user_id' => $item->referrer_id, 'level' => $tmp->level]) }}">{{ $tmp->name }}</a>
                            @endif
                        </td>
                        <td>{{ $referrer->get($item->referrer_id)?  \App\Modules\User\Models\User::$levelShow[$referrer->get($item->referrer_id)->level]:"" }}</td>
{{--                        <td>{{ $personal->get($item->personal_id)? $personal->get($item->personal_id)->name:""}}</td>--}}
{{--                        <td>{!! $item->has_order ? "<i class='fa fa-check'></i>":""  !!}</td>--}}
                        <td>{{ $item->created_at }}</td>
                        <td>{{ $item->last_time }}</td>
                        @if($item->level == 2)
                            <td>
                                @if(!is_null($tmp = $referrer->get($item->personal_id)))
                                    <a href="{{ route('admin.user.index', ['user_id' => $item->personal_id, 'level' => $tmp->level]) }}"> {{ $tmp->name }}</a>
                                @endif
                            </td>
                        @endif
                        <td>
                            @can("recharge_record_recharge")
                                <a class="btn btn-primary btn-xs" href="{{ route('admin.rechargeRecord.recharge',['user_id' => $item->id]) }}"><i class="fa fa-edit"></i> 充值</a>
                            @endcan


                            @can("user-member")
                            @if($item->level == 1)
                                <a class="btn btn-info btn-xs" href="{{ route('admin.user.member', ['id' => $item->id]) }}"><i class="fa fa-list-alt"></i> 会员</a>
                            @endif
                            @endcan
                            @can("user-level{$level}-level")
                            <a class="btn btn-primary btn-xs changeLevel" data-url="{{ route('admin.user.level', ['id' => $item->id]) }}" data-level="{{ $item->level }}"><i class="fa fa-user"></i> 修改身份</a>
                            @endcan
{{--                            @if($item->level == 2)--}}
{{--                                <a class="btn btn-primary btn-xs changePersonal" data-url="{{ route('admin.user.personal', ['id' => $item->id]) }}"><i class="fa fa-minus"></i> 修改店员</a>--}}
{{--                            @endif--}}

                            @if($item->level == 2)
                                @can('user-apply')
                                <a class="btn btn-primary btn-xs" href="{{ route('admin.user.partner.application', ['id' => $item->id]) }}" target="_blank"><i class="fa fa-eidt"></i> 查看申请</a>
                                @endcan
                                @can('user-edit-referrer')
                                <a class="btn btn-primary btn-xs changeReferrer" data-url="{{ route('admin.user.personal', ['id' => $item->id]) }}"><i class="fa fa-link"></i> 修改邀请人</a>
                                @endcan
                            @endif
                            @can("user-level{$level}-edit")
                            <a class="btn btn-primary btn-xs" href="{{ route('admin.user.edit', ['id' => $item->id, 'level' => $item->level]) }}"><i class="fa fa-edit"></i> 编辑</a>
                            @endcan
{{--                            @if($item->is_referrer_candidate == 0)--}}
{{--                                <a class="btn btn-success btn-xs" data-method="post" href="{{ route('admin.user.candidate', ['id' => $item->id])}}"><i class="fa  fa-circle-o"></i> 设置为候选人</a>--}}
{{--                            @else--}}
{{--                                <a class="btn btn-success btn-xs" data-method="post" href="{{ route('admin.user.candidate', ['id' => $item->id])}}" href=""><i class="fa  fa-circle"></i> 取消候选人</a>--}}
{{--                            @endif--}}
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            {{ $data->appends(['search' => $search, 'searchReferrer' => $searchReferrer, 'level' => $level])->links() }}
        </div>

        <div class="modal fade loading custom-hide" id="hideForm" tabindex="-1" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" style="text-align: left">
                    <form class='form'  action="" method="POST">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                        <div class="form-group">
                            <label for="level">身份：</label>
                            <select class="form-control" name="level" id="level">
                                @foreach(\App\Modules\User\Models\User::$levelShow as $key => $value)
                                    <option value="{{ $key }}">{{ $value }}</option>
                                @endforeach
                            </select>
                        </div>
                        <div class="hidden" id="identity-div">
                            <div class="form-group">
                                <label for="identity">选择职位：</label>
                                <select class="form-control" name="identity" id="identity">
                                    <option value="{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_PRIMARY }}">{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_PRIMARY }}</option>
                                    <option value="{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_MIDDEL }}">{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_MIDDEL }}</option>
                                    <option value="{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_SENIOR }}">{{ \App\Modules\User\Models\User::EMPLOYEE_IDENTITY_SENIOR }}</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="shop_id" class="control-label">所属商铺</label>
                                <select class="form-control select2" multiple id="shop_id" name="shop_id[]">
                                    @foreach($shop as $value)
                                        <option value="{{ $value->id }}">{{ $value->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="identity">是否开通掌上管理权限：</label>
                                <select class="form-control" name="is_palm_management" id="is_palm_management">
                                    <option value="0">否</option>
                                    <option value="1">是</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label for="position" class="control-label">职位</label>
                                <input class="form-control" name="position" type="text" value="" id="position">
                            </div>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success form-control">提交</button>
                            <br />
                            <br />
                            <button type="button" class="btn" id="closeHideForm">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

{{--        <div class="modal fade loading custom-hide" id="hideForm2" tabindex="-1" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">--}}
{{--            <div class="content invoice" style="text-align: left">--}}
{{--                <form class='form'  action="" method="POST">--}}
{{--                    <input type="hidden" name="_token" value="{{ csrf_token() }}" />--}}
{{--                    <div class="form-group">--}}
{{--                        <label for="personal_id">选择店员：</label>--}}
{{--                        <select class="form-control" name="personal_id">--}}
{{--                            @foreach($personal as $value)--}}
{{--                                <option value="{{ $value->id }}">{{ $value->name }}</option>--}}
{{--                            @endforeach--}}
{{--                        </select>--}}
{{--                    </div>--}}
{{--                    <div class="form-group">--}}
{{--                        <button type="submit" class="btn btn-success form-control">提交</button>--}}
{{--                        <br />--}}
{{--                        <br />--}}
{{--                        <button type="button" class="btn" id="closeHideForm2">关闭</button>--}}
{{--                    </div>--}}
{{--                </form>--}}
{{--            </div>--}}
{{--        </div>--}}

        <div class="modal fade loading custom-hide" id="hideForm3" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" style="text-align: left">
                    <form action="" method="POST">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                        <div class="form-group">
                            <label for="personal_id">选择推荐人：</label>
                            <select class="form-control select2 update-referrer" style="width: 100%" name="personal_id" required="required">
                            </select>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success form-control">提交</button>
                            <br />
                            <br />
                            <button type="button" class="btn" id="closeHideForm3">关闭</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="modal fade loading custom-hide" id="hideForm4" role="dialog" aria-labelledby="hideFormLibel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
            <div class="modal-dialog">
                <div class="content invoice" style="text-align: left">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    </div>
                    <form method="POST" action="{{ route('admin.user.batch_referrer') }}">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}" />
                        <div class="form-group">
                            <label for="personal_id">老推荐人：</label>
                            <select class="form-control select2 update-referrer" style="width: 100%" name="old_personal_id" required="required">
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="personal_id">新推荐人：</label>
                            <select class="form-control select2 update-referrer" style="width: 100%" name="new_personal_id" required="required">
                            </select>
                        </div>
                        <div class="form-group">
                            <button type="submit" class="btn btn-success form-control">提交</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
@endsection
@push("stylesheets")
    <style>
        .select2-container--default .select2-selection--multiple .select2-selection__choice {
            color: gray;
        }
    </style>
@endpush
@push('scripts')
    <script>
        $('#shop_id.select2').select2({width: "100%"});
        // $('#start').datetimepicker({format:"YYYY-MM-DD"});
        // $('#end').datetimepicker({format:"YYYY-MM-DD"});
        $("#start").datetimepicker({format: "Y-m-d",timepicker:false});
        $("#end").datetimepicker({format: "Y-m-d",timepicker:false});
        $.datetimepicker.setLocale('zh');

        $('.changeLevel').on('click', function () {
            $("#hideForm form").attr('action', $(this).attr('data-url'));
            var level = $(this).attr('data-level');
            $("#level").find("option[value='"+ level +"']").attr('selected', true);
            $("#hideForm").modal('show');
        });
        $('#closeHideForm').on('click', function () {
            $("#hideForm").modal('hide');
        });

        // $('.changePersonal').on('click', function () {
        //     $("#hideForm2 form").attr('action', $(this).attr('data-url'));
        //     $("#hideForm2").modal('show');
        // });
        // $('#closeHideForm2').on('click', function () {
        //     $("#hideForm2").modal('hide');
        // });

        $('.changeReferrer').on('click', function () {
            $("#hideForm3 form").attr('action', $(this).attr('data-url'));
            $("#hideForm3").modal('show');
        });
        $('#closeHideForm3').on('click', function () {
            $("#hideForm3 form").attr('action', $(this).attr('data-url'));
            $("#hideForm3").modal('hide');
        });

        $('.batchChangeReferrer').on('click', function () {
            $("#hideForm4").modal('show');
        });

        $('#level').on('change', function () {
            if ($(this).val() == 3) {
                $('#identity-div').removeClass('hidden');
            } else {
                $('#identity-div').addClass('hidden');
            }
        });

        $('.update-referrer').select2({
            ajax: {
                dataType: 'json',
                delay: 250,
                url: function () {
                    return '{{ route('admin.search_user', ['level' => 3]) }}';
                },
                processResults: function (data) {
                    return {
                        results: $.map(data, function(row, key) {
                            return {
                                id: key,
                                text: row
                            };
                        })
                    };
                }
            }
        });

        $(".select-button").on('click', function () {
            var form = $("#select-form");
            form.attr('method', 'get');
            form.attr('action', $(this).data('action'))
            form.submit();
        });
        $(".export").on('click', function () {
            $(this).prop('disabled', true);
            var form = $("#select-form");
            form.attr("method", "POST");
            form.attr('action', $(this).data('action'))
            form.submit();
        });
    </script>
@endpush
